<?php

namespace app\common\model\trade;

use think\Model;

class Log extends Model
{
    // 表名
    protected $name = 'trade_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'finishtime_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['waitting' => __('Status waitting'),'affirm' => __('Status affirm'),'finish' => __('Status finish'),'dispute' => __('Status dispute'),'solve' => __('Status solve')];
    }     


    public function getFinishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['finishtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setFinishtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public static function Log($trade_no,$buy_user_id,$buy_username,$buy_trade_id,$sell_user_id,$sell_username,$sell_trade_id,$buy_num,$sell_num,$exch_rate,$status='waitting',$action,$symbol,$tosymbol,$pay='')
    {
        self::create([
            'trade_no'      =>  $trade_no,
            'buy_user_id'   =>  $buy_user_id,
            'buy_username'  =>  $buy_username,
            'buy_trade_id'  =>  $buy_trade_id,
            'sell_user_id'  =>  $sell_user_id,
            'sell_username' =>  $sell_username,
            'sell_trade_id' =>  $sell_trade_id,
            'buy_num'       =>  $buy_num,
            'sell_num'      =>  $sell_num,
            'exch_rate'     =>  $exch_rate,
            'status'        =>  $status,
            'action'        =>  $action,
            'symbol'        =>  $symbol,
            'tosymbol'      =>  $tosymbol,
            'pay'           =>  $pay
        ]);
    }
}
